package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Department;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 科室表(Department)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:02
 */
@RestController
@RequestMapping("department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Department selectOne(Integer id) {
        return this.departmentService.queryById(id);
    }
    
    @RequestMapping("getDepartmentList")
    public ResultDTO getDepartmentList(@RequestBody Department department)
    {
        IPage<Department> ipage = departmentService.queryDepartmentList(department);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertDepartment")
    public ResultDTO insertDepartment(@RequestBody Department department)
    {
        Department tmpDepartment = departmentService.insert(department);
        if (tmpDepartment != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getDepartmentById")
    public ResultDTO getDepartmentById(@RequestBody Map<String, Integer> map)
    {
        int departmentId = map.get("departmentId");
        Department department = departmentService.queryById(departmentId);
        if (department==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(department);
    }

    @RequestMapping("updateDepartment")
    public ResultDTO updateDepartment(@RequestBody Department department)
    {
        if (department==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (departmentService.update(department)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteDepartment")
    public ResultDTO deleteDepartment(@RequestBody Map<String,Integer> map)
    {
        int departmentId = map.get("departmentId");
        if (departmentService.deleteById(departmentId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }
}